<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\UserNotify;
use app\common\tool\Result;
use think\Response;

class Notify extends Base
{
    public function typeList(): Response
    {
        $total = 0;
        $type = getNotifyType();
        $list = [];
        foreach ($type as $key => $item) {
            $count = UserNotify::where(['type_id' => $key, 'uid' => $this->user->id, 'status' => 0])->count();
            $list[] = [
                'id' => $key,
                'title' => $item,
                'count' => $count
            ];
            $total += $count;
        }
        $result['total'] = $total;
        $result['list'] = $list;

        return Result::success('success', $result);
    }

    public function index(): Response
    {
        $type_id = $this->request->get('type_id', 1);
        $map = ['uid' => $this->user->id, 'type_id' => $type_id, 'status' => 0];
        UserNotify::update(['status' => 1], $map);
        unset($map['status']);

        return Result::fastReturn(UserNotify::getPageList($map, 'id,title,content,order_id,create_time',
            $this->getPage(), $this->getLimit(), false));
    }
}